package com.sgb.kjwl.view.ui.fragment.home;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.google.android.material.tabs.TabLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentCarrierDemandBinding;
import com.sgb.kjwl.model.entity.transport.CarriageInfo;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.transport.GoodsSourceFilterEntity;
import com.sgb.kjwl.model.entity.transport.GoodsSourceLocationEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.utils.TextViewUtils;
import com.sgb.kjwl.utils.VehicleConstant;
import com.sgb.kjwl.view.event.LogisticsEvent;
import com.swgk.core.event.MenuEvent;
import com.sgb.kjwl.view.ui.activity.MainActivity;
import com.sgb.kjwl.view.ui.activity.transport.CarriageDetailActivity;
import com.sgb.kjwl.view.ui.adapter.transport.CarriageListMultiTypeAdapter;
import com.sgb.kjwl.view.widget.CarsInfoMorePopWindow3;
import com.sgb.kjwl.view.widget.GoodsTypePopWindow3;
import com.sgb.kjwl.view.widget.SendGoodsLocationPopWindow3;
import com.sgb.kjwl.view.widget.ThreeLevelAreaWindow;
import com.sgb.kjwl.viewmodel.carrierDemand.CarrierDemandFtvModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.event.EventConstant;
import com.swgk.core.recyclerview.decoration.RecyclerViewItemDecoration;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.LogUtil;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * 物流首页-竞价找车
 */

public class CarrierDemandFragment extends AppLazyFragment {
    CarrierDemandFtvModel viewModel;
    private int LOCATION_TYPE = 0;//0发货地，1目的地

    // 一级联动数据
    public List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> addressEntityList;
    // 二级联动数据
    public List<List<AddressEntity<List<AddressEntity>>>> opt2 = new ArrayList<>();
    // 三级联动数据
    public List<List<List<AddressEntity>>> opt3 = new ArrayList<>();

    private CarsInfoMorePopWindow3 carsInfoMorePopWindow;
    private SendGoodsLocationPopWindow3 sendGoodsLocationPopWindow3;
    private ThreeLevelAreaWindow destinationPopWindow;

    FragmentCarrierDemandBinding binding;

    private List<GoodsInfoTypeEntity3> carsTypeData;
    private List<GoodsInfoTypeEntity3> carsLengthData;
    CarriageListMultiTypeAdapter carrierDemandListAdapter;
    //  private CarrierDemandListAdapter carrierDemandListAdapter; //接口完成 替换数据
    private int page = 1;
    private boolean isLoadMore = false;
    private String carTypeNo = "";
    private String carLengthNo = "";
    private int typePosition = -1;
    private int lengthPosition = -1;
    private GoodsTypePopWindow3 carsTypePopWindow;
    private GoodsTypePopWindow3 carsLengthPopWindow;

    boolean isRefresh = true;
    boolean isShowVideoList = false;
    boolean isProprietary = false;//是否自营
    private ThreeLevelAreaWindow areaWindow;
    private Activity _mActivity;

    public static CarrierDemandFragment newInstance(boolean isRefresh, boolean isShowVideo) {
        CarrierDemandFragment fragment = new CarrierDemandFragment();
        Bundle args = new Bundle();
        args.putBoolean("isRefresh", isRefresh);
        args.putBoolean("isShowVideo", isShowVideo);
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * 前台主页--个人物流需求--承运需求
     *
     * @return
     */
    public static CarrierDemandFragment newInstance(String websiteType, String websiteNo) {
        CarrierDemandFragment fragment = new CarrierDemandFragment();
        Bundle args = new Bundle();
        args.putString("websiteType", websiteType);
        args.putString("websiteNo", websiteNo);
        args.putBoolean("isRefresh", true);
        args.putBoolean("isShowVideo", false);
        args.putBoolean("isProprietary", true);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
    }

    @Override
    public void onAttach(@NonNull @NotNull Context context) {
        super.onAttach(context);
        _mActivity = (Activity) context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_carrier_demand, container, false);
        binding.setViewModel(viewModel);
        if (isProprietary) {
            binding.line2.setVisibility(View.GONE);
        }
        return binding.getRoot();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewModel = new ViewModelProvider(this).get(CarrierDemandFtvModel.class);
        if (getArguments() != null) {
            isRefresh = getArguments().getBoolean("isRefresh", true);
            isShowVideoList = getArguments().getBoolean("isShowVideo", false);
            isProprietary = getArguments().getBoolean("isProprietary", false);
            viewModel.websiteNo = getArguments().getString("websiteNo");
            viewModel.websiteType = getArguments().getString("websiteType");
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    public void initViews() {
        binding.setOnClick(this);
        binding.carsMoreLayout.setOnClickListener(this);
        binding.typeMore.setOnClickListener(this);
        binding.lengthMore.setOnClickListener(this);
        binding.carsList.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        binding.carsList.addItemDecoration(new RecyclerViewItemDecoration(getResources().getDimension(R.dimen.dp5)));

        if (carrierDemandListAdapter == null) {
            carrierDemandListAdapter = new CarriageListMultiTypeAdapter(new ArrayList<>());
        }
        binding.carsList.setAdapter(carrierDemandListAdapter);
        carrierDemandListAdapter.setOnItemClickListener((adapter, view, position) ->
                CarriageDetailActivity.start(getActivity(), carrierDemandListAdapter.getData().get(position).getCarrierNo(), 1));

        if (!isRefresh) {
            binding.carsListRefreshLayout.setEnableRefresh(false);
            binding.llChangeMap.setVisibility(View.GONE);
        }
        if (isShowVideoList) {
            viewModel.filterEntity.get().setNeedVideo("1");
        }
        binding.carsListRefreshLayout.setEnableLoadMore(true);
        binding.carsListRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isLoadMore = true;
                page = page + 1;
                viewModel.getListData(_mActivity, page);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                isLoadMore = false;
                viewModel.getListData(_mActivity, page);
            }
        });


        //类型
        binding.tabLayoutType.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (null != carsTypeData && carsTypeData.size() > 0) {
                    carTypeNo = carsTypeData.get(tab.getPosition()).getNo();
                    typePosition = tab.getPosition();
                    viewModel.carType.set(carsTypeData.get(tab.getPosition()));
                    filterByCarType();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

        //类型
        carsTypePopWindow = new GoodsTypePopWindow3(getActivity());
        carsTypePopWindow.setBgView(binding.grayLayout);
        carsTypePopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object type, Object key2, Object key3) {
                GoodsInfoTypeEntity3 entity = (GoodsInfoTypeEntity3) type;
                typePosition = (int) key2;
                carTypeNo = entity.getNo();
                binding.tabLayoutType.selectTab(binding.tabLayoutType.getTabAt(typePosition));
                viewModel.carType.set(entity);
                filterByCarType();
            }
        });
        carsTypePopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                binding.typeMore.setImageResource(R.mipmap.icon_tab_unfold);
            }
        });

        //车长
        binding.tabLayoutLength.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (null != carsLengthData && carsLengthData.size() > 0) {
                    carLengthNo = carsLengthData.get(tab.getPosition()).getNo();
                    lengthPosition = tab.getPosition();
                    viewModel.carLength.set(carsLengthData.get(tab.getPosition()));
                    getData();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

        //车长
        carsLengthPopWindow = new GoodsTypePopWindow3(getActivity());
        carsLengthPopWindow.setBgView(binding.grayLayout);
        carsLengthPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object type, Object key2, Object key3) {
                GoodsInfoTypeEntity3 entity = (GoodsInfoTypeEntity3) type;
                lengthPosition = (int) key2;
                carLengthNo = entity.getNo();
                //  binding.tabLayoutLength.setScrollPosition(lengthPosition,0,true);
                binding.carLen.setText(entity.getName());
                viewModel.carLength.set(entity);
                getData();
            }
        });
        carsLengthPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                String longs = binding.carLen.getText().toString();
                if (!longs.equals("车长")) {
                    binding.llCarlen.setBackgroundResource(R.drawable.shape_theme_coner);
                    TextViewUtils.changeTextView(getActivity(), binding.carLen, R.mipmap.ic_down_arrow, true);
                } else {
                    binding.llCarlen.setBackgroundResource(R.drawable.shape_gray_coner);
                    TextViewUtils.changeTextView(getActivity(), binding.carLen, R.mipmap.ic_down_arrow_n, false);
                }

            }
        });

        //收货省份popWindow
        destinationPopWindow = new ThreeLevelAreaWindow(getActivity()) {
            @Override
            protected void resultContent(String name, String simpleName) {
                binding.tvDestination.setText(simpleName);
            }
        };
        destinationPopWindow.setBgView(binding.grayLayout);
        destinationPopWindow.setAreaSingle();
        destinationPopWindow.setCitySingle();
        destinationPopWindow.setProvinceSingle();
        destinationPopWindow.setShowSimpleName(true);
        destinationPopWindow.setAllSingleSelect(true);
        destinationPopWindow.setOnlyShowProAndCity(true);
        destinationPopWindow.setOnResultClick(new ThreeLevelAreaWindow.OnResultClickProxy() {
            @Override
            protected void result(GoodsSourceLocationEntity entity) {
                viewModel.purposeLocation.set(entity);

                page = 1;
                isLoadMore = false;

                viewModel.getListData(_mActivity, page);

                LogUtil.v("goodsInfo", "货物目的地址=" + entity.getProvinceName() + "==" + entity.getCityName() + "==" + entity.getAreaName());
            }
        });
        destinationPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                String city = binding.tvDestination.getText().toString();
                if (!city.equals("目的地")) {
                    binding.llDestination.setBackgroundResource(R.drawable.shape_theme_coner);
                    TextViewUtils.changeTextView(getActivity(), binding.tvDestination, R.mipmap.ic_down_arrow, true);
                } else {
                    binding.llDestination.setBackgroundResource(R.drawable.shape_gray_coner);
                    TextViewUtils.changeTextView(getActivity(), binding.tvDestination, R.mipmap.ic_down_arrow_n, false);
                }

            }
        });


        //更多
        carsInfoMorePopWindow = new CarsInfoMorePopWindow3(getActivity());
        carsInfoMorePopWindow.setBgView(binding.grayLayout);
        carsInfoMorePopWindow.setOnDismissListener(() -> binding.goodsMoreIv.setSelected(false));
        carsInfoMorePopWindow.setOnResultCallBack(new BasePopWindow.OnResultCallBack() {
            @Override
            public void result(Object result) {
                GoodsSourceFilterEntity filterEntity = (GoodsSourceFilterEntity) result;
                viewModel.filterEntity.set(filterEntity);
                page = 1;
                isLoadMore = false;
                viewModel.getListData(_mActivity, page);
            }
        });
  /*      binding.carsList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                //获得recyclerView的线性布局管理器
                LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                //获取到第一个item的显示的下标  不等于0表示第一个item处于不可见状态 说明列表没有滑动到顶部 显示回到顶部按钮
                int firstVisibleItemPosition = manager.findFirstVisibleItemPosition();
                int endVisibleItemPosition = manager.findLastVisibleItemPosition();
                // 当不滚动时
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {

                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {

            }
        });*/
        areaWindow = new ThreeLevelAreaWindow(getActivity()) {
            @Override
            protected void resultContent(String name, String simpleName) {
                binding.tvSend.setText(simpleName);
            }
        };

        areaWindow.setOnResultClick(new ThreeLevelAreaWindow.OnResultClickProxy() {
            @Override
            protected void result(GoodsSourceLocationEntity entity) {
                viewModel.sendLocation.set(entity);
                page = 1;
                isLoadMore = false;
                viewModel.getListData(_mActivity, page);
                LogUtil.v("goodsInfo", "发货物地址=" + entity.getProvinceName() + "==" + entity.getCityName() + "==" + entity.getAreaName());
            }
        });
        areaWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                String sendCity = binding.tvSend.getText().toString();
                if (!sendCity.equals("发货地")) {
                    binding.llSend.setBackgroundResource(R.drawable.shape_theme_coner);
                    TextViewUtils.changeTextView(getActivity(), binding.tvSend, R.mipmap.ic_down_arrow, true);
                } else {
                    binding.llSend.setBackgroundResource(R.drawable.shape_gray_coner);
                    TextViewUtils.changeTextView(getActivity(), binding.tvSend, R.mipmap.ic_down_arrow_n, false);
                }
            }
        });
        areaWindow.setAreaSingle();
        areaWindow.setCitySingle();
        areaWindow.setProvinceSingle();
        areaWindow.setShowSimpleName(true);
        areaWindow.setAllSingleSelect(true);
        areaWindow.setOnlyShowProAndCity(true);
        areaWindow.setBgView(binding.grayLayout);
    }


    @Override
    protected void onLazyLoad() {
        initViews();
        loadData();
    }

    private void getData() {
        page = 1;
        isLoadMore = false;
        viewModel.getListData(_mActivity, page);
    }

    //列表
    public void getCarrierInfoData(List<CarriageInfo> list, boolean noMoreData) {
        binding.carsListRefreshLayout.finishRefresh();
        if (isLoadMore) {
            binding.carsListRefreshLayout.finishLoadMore();
            if (list.size() > 0) {
                carrierDemandListAdapter.addData(list);
            } else {
                binding.carsListRefreshLayout.finishLoadMoreWithNoMoreData();
            }
            binding.carsList.setVisibility(View.VISIBLE);

        } else {
            binding.carsListRefreshLayout.finishRefresh();
            if (list.size() > 0) {
                binding.carsList.setVisibility(View.VISIBLE);
                binding.carsListRefreshLayout.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                carrierDemandListAdapter.setDatas(list);
                if (list.size() < 10) {
                    binding.carsListRefreshLayout.finishLoadMoreWithNoMoreData();
                }
            } else {
                carrierDemandListAdapter.setDatas(null);
                binding.carsList.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
        binding.carsListRefreshLayout.setNoMoreData(noMoreData);
    }


    private void showSendPopWindow() {
        String proName = "不限", cityName = "不限", areaName = "不限";
        areaWindow.setDataAndPosition(addressEntityList, proName, cityName, areaName, true, true);
        showPopup(areaWindow);
    }

    private void showDestinationPopWindow() {
        destinationPopWindow.setData(addressEntityList, true);
        showPopup(destinationPopWindow);
    }

    private void showMoreFilterPopWindow() {
        carsInfoMorePopWindow.showPopwindowRight(binding.carsMoreLayout);
        carsInfoMorePopWindow.setData(carsTypeData, carsLengthData);
    }

    private void loadData() {
        binding.tvSend.setText("全国");
        binding.llSend.setBackgroundResource(R.drawable.shape_theme_coner);
        TextViewUtils.changeTextView(getActivity(), binding.tvSend, R.mipmap.ic_down_arrow, true);
        viewModel.getCarTypeList();

        viewModel.ldTypeList.observe(this, this::getCarsTypeData);
        viewModel.ldLengthList.observe(this, this::getCarsLengthData);
        viewModel.ldAdress.observe(this, this::initAddressData);
        viewModel.ldCarriageInfo.observe(this, list -> {
            getCarrierInfoData(list.getList(), list.isLastPage());
        });
        viewModel.ldErrorList.observe(this, error -> {
            getCarrierInfoData(new ArrayList<CarriageInfo>(), false);
        });
    }

    private void showCarsTypePopWindow() {
        EventBus.getDefault().post(LogisticsEvent.make().setMessageTag(EventConstant.EVENT_LOGISTICS_WINDOW_TOP));
        carsTypePopWindow.setData(carsTypeData, typePosition);
        new Handler().postDelayed(() -> carsTypePopWindow.showPopwindow(binding.tabLayoutType, 0, 0), 400);
    }

    private void showCarsLengthPopWindow() {
        carsLengthPopWindow.setData(VehicleConstant.sortCarLength(carsLengthData), lengthPosition);
        showPopup(carsLengthPopWindow);
    }

    /**
     * 展示选择弹窗
     */
    public void showPopup(BasePopWindow pop) {
        if (getActivity() instanceof MainActivity) {
            EventBus.getDefault().post(LogisticsEvent.make().setMessageTag(EventConstant.EVENT_LOGISTICS_WINDOW_TOP));
            new Handler().postDelayed(() -> pop.showPopwindow(binding.line4, 0, 0), 300);
        } else {
            pop.showPopwindow(binding.line4, 0, 0);
        }
    }

    @Override
    public void onClick(View v) {
        if (ClickUtils.isDoubleClick()) {
            return;
        }
        switch (v.getId()) {
            case R.id.cars_more_layout:
                setOptionStatus(4);
                if (null != carsTypeData && carsTypeData.size() > 0 && null != carsLengthData && carsLengthData.size() > 0) {
                    showMoreFilterPopWindow();
                }
                break;

            case R.id.type_more:
                if (null != carsTypeData && carsTypeData.size() > 0) {
                    binding.typeMore.setImageResource(R.mipmap.icon_tab_pack_up);
                    showCarsTypePopWindow();
                }
                break;
            case R.id.length_more:
                if (null != carsLengthData && carsLengthData.size() > 0) {
                    showCarsLengthPopWindow();
                }
                break;
            case R.id.ll_send:

                LOCATION_TYPE = 0;
                if (null != addressEntityList && addressEntityList.size() > 0) {
                    showSendPopWindow();
                } else {
                    viewModel.getAddressModel();
                }
                setOptionStatus(1);
                break;
            case R.id.ll_destination:

                LOCATION_TYPE = 1;
                if (null != addressEntityList && addressEntityList.size() > 0) {
                    showDestinationPopWindow();
                } else {
                    viewModel.getAddressModel();
                }
                setOptionStatus(2);
                break;
            case R.id.ll_carlen:

                if (null != carsLengthData && carsLengthData.size() > 0) {
                    showCarsLengthPopWindow();
                }
                setOptionStatus(3);
                break;
        }
    }

    private void setOptionStatus(int index) {
        switch (index) {
            case 1:
                binding.llSend.setBackgroundResource(R.drawable.shape_gray_coner);
                TextViewUtils.changeTextView(getActivity(), binding.tvSend, R.mipmap.ic_up_arrow_n, false);
                break;
            case 2:
                binding.llDestination.setBackgroundResource(R.drawable.shape_gray_coner);
                TextViewUtils.changeTextView(getActivity(), binding.tvDestination, R.mipmap.ic_up_arrow_n, false);
                break;
            case 3:
                binding.llCarlen.setBackgroundResource(R.drawable.shape_gray_coner);
                TextViewUtils.changeTextView(getActivity(), binding.carLen, R.mipmap.ic_up_arrow_n, false);
                break;
            case 4:
                binding.tvMore.setSelected(true);
                binding.goodsMoreIv.setSelected(true);
                break;
        }
    }

    public void cityByLoction(double lat, double lon) {
        //通过经纬度获取位置信息
        GeocodeSearch geocodeSearch = new GeocodeSearch(getActivity());
        // 第一个参数表示一个LatLng，第二参数表示范围多少米，第三个参数表示是火系坐标系还是GPS原生坐标系
        RegeocodeQuery query = new RegeocodeQuery(new LatLonPoint(lat,
                lon), 200, GeocodeSearch.AMAP);
        geocodeSearch.getFromLocationAsyn(query);
        //搜索位置信息
        geocodeSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
            @Override
            public void onRegeocodeSearched(RegeocodeResult result, int code) {
                if (code != 1000) return;
                RegeocodeAddress address = result.getRegeocodeAddress();
                if (address != null) {

                    refreshSendCitySelect(address.getProvince(), address.getCity(), address.getDistrict());


                }
            }

            @Override
            public void onGeocodeSearched(GeocodeResult result, int code) {

            }
        });
    }

    //地图移动时刷新发货地文字
    private void refreshSendCitySelect(String province, String city, String district) {
        destinationPopWindow.setDataAndPosition(addressEntityList, province, city, district, true, false);
        BaseAreaEntity[] entities = destinationPopWindow.findCode(province, city, district);
        binding.tvSend.setText(entities[1].getSimpleName());
    }

    public void initAddressData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> datas) {
        addressEntityList = datas;
        if (LOCATION_TYPE == 1) {
            showDestinationPopWindow();
        } else if (LOCATION_TYPE == 0) {
            showSendPopWindow();
        }
    }


    //获取车型
    public void getCarsTypeData(List<GoodsInfoTypeEntity3> list) {
        if (null != list && list.size() > 0) {
            carsTypeData = list;
            binding.tabLayoutType.removeAllTabs();
            for (int i = 0; i < list.size(); i++) {
                binding.tabLayoutType.addTab(binding.tabLayoutType.newTab().setText(list.get(i).getName()));
            }

        } else {
            MToast.showToast(getActivity(), "数据未加载");
        }
    }

    //获取车长
    public void getCarsLengthData(List<GoodsInfoTypeEntity3> list) {
        if (null != list && list.size() > 0) {
            carsLengthData = list;
          /*  for(int i=0;i<list.size();i++){
                binding.tabLayoutLength.addTab(binding.tabLayoutLength.newTab().setText(list.get(i).getName()));
            }*/
        }
    }

    /**
     * 根据车型筛选数据
     */
    private void filterByCarType() {
        //切换车型时重置车长，填充车长筛选数据
        lengthPosition = 0;
        viewModel.carLength.set(null);
        //刷新车长数据
        if (carsTypeData != null && carsTypeData.size() > 0) {
            carsLengthData = carsTypeData.get(typePosition).getCarLenList();

        }
        //刷新
        getData();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MenuEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_ORDER_FINISH) {
            page = 1;
            isLoadMore = false;
            viewModel.getListData(_mActivity, page);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refresh(LogisticsEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_LOGISTICS_REFRESH) {
//            if (ArrayUtil.isNullOrEmpty(carsTypeData)) {
            //获取数据
            viewModel.getCarTypeList();
//            }
            page = 1;
            isLoadMore = false;
            viewModel.getListData(_mActivity, page);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }
}
